package org.water.garden.core;

import android.content.ContentResolver;
import android.database.Cursor;
import android.net.Uri;
import android.provider.Im.Account;
import android.provider.Im.Provider;

public class ImUtils {

	public static String queryProviderName(ContentResolver resolver, Uri accountUri){
		String providerName = null;
		
		String accId = accountUri.getLastPathSegment();
		Cursor cur = resolver.query(Account.CONTENT_URI, new String[]{Account.PROVIDER}, 
				Account._ID + "=?", new String[]{accId}, null);

		if(cur.first()){
			Integer providerId = cur.getInt(0);
			cur.close();
			
			cur = resolver.query(Provider.CONTENT_URI, new String[]{Provider.NAME}, 
					Provider._ID + "=?", new String[]{providerId.toString()}, null);
			
			if(cur.first()){
				providerName = cur.getString(0);
			}
		}
		
		return providerName;
	}

	public static Cursor queryAccount(ContentResolver resolver, Uri accountUri, String[] projection){
		String accId = accountUri.getLastPathSegment();
		return resolver.query(Account.CONTENT_URI, projection, 
				Account._ID + "=?", new String[]{accId}, null);
	}
}
